package com.jk.smartelderlycare.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.jk.smartelderlycare.dto.SysLabel;
import com.jk.smartelderlycare.dto.SysRole;
import com.jk.smartelderlycare.dto.sysVideo;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.security.LoginUser;
import com.jk.smartelderlycare.service.SysVideoServiceFeign;
import com.jk.smartelderlycare.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysVideoController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/15 9:27
 */
@Api(tags = "视频管理模块")
@Slf4j
@RestController
@RequestMapping(value = "video")
public class SysVideoController {

    @Autowired
    private SysVideoServiceFeign sysVideoServiceFeign;
    /**
     * @description: 视频列表后端分页查询
     * @param: [video, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.sysVideo>>
     * @author hhk
     * @date: 2024/11/15 9:32
     */
    @ApiOperation(value = "视频列表后端分页查询",httpMethod = "POST",notes = "视频列表后端分页查询")
    @PostMapping(value = "findSysVideoPaginationList")
    public ResponseResult<PagedResponse<sysVideo>>findSysVideoPaginationList(@RequestBody sysVideo video, @RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize){

        return new ResponseResult<>(200,"查询成功",sysVideoServiceFeign.findSysVideoPaginationList(video,pageNum,pageSize));
    }
    /**
     * @description: 新增视频
     * @param: [video]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 9:33
     */
    @ApiOperation(value ="新增视频",httpMethod = "POST",notes = "新增视频")
    @PostMapping(value = "addSysVideo")
    public ResponseResult addSysVideo (@RequestBody sysVideo video){
        try {
            video.setVideoLike("1001");
            video.setVideoCollection("200");
            video.setVideoShare("100");
            video.setVideoComments("6");
            sysVideoServiceFeign.addSysVideo(video);
            return new ResponseResult(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"添加失败");
        }

    }
    /**
     * @description: 根据id回显一条数据
     * @param: [videoId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 9:37
     */
    @ApiOperation(value = "根据id回显一条数据",httpMethod = "GET",notes = "根据id回显一条数据")
    @GetMapping(value = "findSysVideoByVideoId")
    public ResponseResult<sysVideo> findSysVideoByVideoId(@RequestParam(value ="videoId") Long videoId){
            return new ResponseResult<>(200,"回显成功", sysVideoServiceFeign.findSysVideoByVideoId(videoId));

    }
    /**
     * @description: 根据id删除一条数据
     * @param: [videoId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 9:39
     */
    @ApiOperation(value = "根据id删除一条数据",httpMethod = "DELETE",notes = "根据id删除一条数据")
    @DeleteMapping(value = "deleteSysVideoByVideoId")
    public ResponseResult deleteSysVideoByVideoId(@RequestParam Long videoId){
        try {
            sysVideoServiceFeign.deleteSysVideoByVideoId(videoId);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }
    /**
     * @description: 修改视频
     * @param: [video]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 9:40
     */
    @ApiOperation(value = "修改视频",httpMethod = "PUT",notes = "修改视频")
    @PutMapping(value = "updateSysVideo")
    private ResponseResult updateSysVideo(@RequestBody sysVideo video){
        try {
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            video.setCreateDate(new Date());
            video.setModifyUser(loginUser.getUser().getUserName());
            sysVideoServiceFeign.updateSysVideo(video);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    /**
     * @description: 标签动态下拉
     * @param: []
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<java.util.List<com.jk.smartelderlycare.dto.SysLabel>>
     * @author hhk
     * @date: 2024/11/15 9:43
     */
    @ApiOperation(value = "标签动态下拉",httpMethod = "GET",notes = "标签动态下拉")
    @GetMapping(value = "findSysLabelList")
    private ResponseResult<List<SysLabel>>findSysLabelList(){
      return new ResponseResult<>(200,"查询成功", sysVideoServiceFeign.findSysLabelList());

    }
    /** 
     * @description: 批量删除视频
     * @param: [ids]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 11:13
     */ 
    @ApiOperation(value = "批量删除视频",httpMethod = "DELETE",notes = "批量删除视频")
    @DeleteMapping(value = "deleteSysVideoByIds")
    private ResponseResult deleteSysVideoByIds(@RequestParam List<Integer> ids){
        try {
            sysVideoServiceFeign.deleteSysVideoByIds(ids);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }

    /**
     * @description: 上传图片
     * @param: [file, request]
     * @return: java.lang.String
     * @author hhk
     * @date: 2024/11/7 10:54
     */
    @ApiOperation(value = "上传图片",httpMethod = "POST",notes = "上传图片")
    @PostMapping("uploadImg")
    public String uploadImg(MultipartFile file, HttpServletRequest request) {
        return FileUtil.uploadFile(file,request);
    }

    /**
     * @description: 上传图片
     * @param: [file, request]
     * @return: java.lang.String
     * @author hhk
     * @date: 2024/11/7 10:54
     */
    @ApiOperation(value = "上传视频",httpMethod = "POST",notes = "上传视频")
    @PostMapping("uploadVideo")
    public String uploadVideo(MultipartFile file, HttpServletRequest request) {
        return FileUtil.uploadFile(file,request);
    }
    /**
     * @description: 信息导出
     * @param: [video, response]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 11:38
     */
    @ApiOperation(value = "信息导出", httpMethod = "POST", notes = "信息导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestBody sysVideo video, HttpServletResponse response){
        List<sysVideo> videoList = sysVideoServiceFeign.findSysVideoList();

        //.xlsx格式
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        response.setContentType("application/octet-stream; charset=UTF-8");


        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("postsInfo", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //设置sheet名
            ExportParams params = new ExportParams();
            params.setSheetName("视频信息表");
            //设置标题
            params.setTitle("视频信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, sysVideo.class, videoList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * @description: 信息导入
     * @param: [file]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 11:38
     */
    @ApiOperation(value = "信息导入", httpMethod = "POST", notes = "信息导入")
    @PostMapping(value = "importExcel")
    public ResponseResult importExcel(@RequestPart("file") MultipartFile file) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(2);
            List<sysVideo> list = ExcelImportUtil.importExcel(file.getInputStream(), sysVideo.class, params);
            list.forEach(video -> {

                sysVideoServiceFeign.addSysVideo(video);
            });
            return new ResponseResult(200, "导入用户信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "导入用户信息失败！");
        }
    }

















}
